package com.itheima.controller.home;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.api.Sms.SmsHomeRecommendSubjectService;
import com.itheima.vo.domain.AjaxResult;
import com.itheima.vo.domain.TableDataInfo;
import com.itheima.vo.pojo.SmsHomeRecommendProduct;
import com.itheima.vo.pojo.SmsHomeRecommendSubject;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/home/recommendSubject")
public class SmsHomeRecommendSubjectController {

    @DubboReference
    SmsHomeRecommendSubjectService recommendSubjectService;


    /*
     * 1.添加首页推荐专题
     * */
    @PostMapping("/create")
    public AjaxResult addIndexRecommend(@RequestBody List<SmsHomeRecommendSubject> smsHomeRecommendSubject)
    {
//
//        SmsHomeRecommendSubject
        boolean save = recommendSubjectService.saveBatch(smsHomeRecommendSubject);

        return AjaxResult.success(save);
    }


    /*
     * 2.批量删除推荐
     * */
    @PostMapping("/delete")
    public AjaxResult BatchRecommend(@RequestParam List<Integer> ids)
    {
        boolean b = recommendSubjectService.removeByIds(ids);
        return AjaxResult.success(ids);

    }

    /*
     * 3.分页
     * */

    @GetMapping("/list")
    public AjaxResult AllPage(Integer pageNum , Integer pageSize , String subjectName, Integer recommendStatus)
    {
        LambdaQueryWrapper<SmsHomeRecommendSubject> lambdaQueryWrapper  =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.ge(subjectName !=null,SmsHomeRecommendSubject::getSubjectId, subjectName);
        lambdaQueryWrapper.like(recommendStatus !=null ,SmsHomeRecommendSubject::getRecommendStatus, recommendStatus);
        TableDataInfo tableDataInfo  = new TableDataInfo();
        Page<SmsHomeRecommendSubject> page = new Page<>(pageNum , pageSize);
        Page<SmsHomeRecommendSubject> page1 = recommendSubjectService.page(page , lambdaQueryWrapper);

        tableDataInfo.setList(page1.getRecords());

        tableDataInfo.setPageNum(pageNum);

        tableDataInfo.setPageSize(pageSize);

        tableDataInfo.setTotal(page1.getTotal());
        tableDataInfo.setTotalPage(page1.getPages());

        return AjaxResult.success(tableDataInfo);
    }


    /*
     * 4.批量修改推荐状态
     * */
    @PostMapping("update/recommendStatus")
    public AjaxResult BatchAlterRecommendStatus(@RequestParam List<Integer> ids , Integer recommendStatus)
    {


        ArrayList<SmsHomeRecommendSubject> Subjects = new ArrayList<>();
        
        for (Integer id : ids) {

            SmsHomeRecommendSubject Subject = new SmsHomeRecommendSubject();

            Subject.setId(id);
            Subject.setRecommendStatus(recommendStatus);
            Subjects.add(Subject);
        }

        boolean b = recommendSubjectService.updateBatchById(Subjects);

        return AjaxResult.success(b);
    }


    /*
     * 5.修改推荐排序
     * */
    @PostMapping("update/sort/{id}")
    public AjaxResult AlterRecommendSort(@PathVariable Integer id ,  Integer sort)
    {

        SmsHomeRecommendSubject Subject = new SmsHomeRecommendSubject();
        Subject.setSubjectId(id);
        Subject.setSort(sort);
        Boolean b =  recommendSubjectService.updateById(Subject);

        return AjaxResult.success(b);
    }

}
